package com.ccz.demo.anotationdemo;

import com.ccz.demo.mongo.Man;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TestClient {


    public static void main(String[] args) throws NoSuchFieldException, InvocationTargetException, IllegalAccessException {
        Man man = new Man();
        man.setAge(20);
        man.setSex("woman");
        man.setName("aaa");
        Class clazz = man.getClass();
        Field[] fields = clazz.getFields();
        Method[] clazzMethods = clazz.getMethods();
        for (Method clazzMethod : clazzMethods) {
            String methodName = clazzMethod.getName();
            if (methodName.startsWith("get") && !methodName.startsWith("getClass")) {
                Object o = clazzMethod.invoke(man);
                String field = methodName.substring(3, methodName.length());
                String fieldStr = new StringBuffer().append(Character.toLowerCase(field.charAt(0))).append(field.substring(1)).toString();
                Field field1 = clazz.getDeclaredField(fieldStr);
                FieldMust annotation = field1.getAnnotation(FieldMust.class);
                if (o != null && annotation != null && annotation.need()) {
                    System.out.println("被注解的属性：" + fieldStr + "，getter方法的值" + o.toString());
                }
            }
        }
    }
}
